package ui.panels;

import entities.User;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
import manager.AdminSysDriver;
import model.EditUserLoginTableModel;
import model.ViewDepartmentComboBoxModel;
import ui.windows.RemedySchedulerFrame;
import ui.windows.ViewUserDialog;

public class UserManagementPanel extends javax.swing.JPanel {

    /** Creates new form UserManagementPanel */
    public UserManagementPanel(RemedySchedulerFrame mainFrame) {
        initComponents();
        this.mainFrame = mainFrame;
        driver = (AdminSysDriver) mainFrame.getDriver();
        includeModel();
    }

    private void includeModel() {
        driver = (AdminSysDriver) mainFrame.getDriver();
        userTableModel = new EditUserLoginTableModel(driver.showUser());
        userTable.setModel(userTableModel);
        userTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        userTable.setFillsViewportHeight(true);
        userTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        DefaultTableCellRenderer cellRenderer1 = new DefaultTableCellRenderer();
        cellRenderer1.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
        cellRenderer1.setBackground(Color.yellow);
        DefaultTableCellRenderer cellRenderer2 = new DefaultTableCellRenderer();
        cellRenderer2.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
        cellRenderer2.setBackground(new Color(100, 50, 102));
        cellRenderer2.setForeground(Color.white);
        userTable.getTableHeader().setFont(new Font(null, Font.BOLD, 10));
        userTable.getColumnModel().getColumn(0).setPreferredWidth(70);
        userTable.getColumnModel().getColumn(1).setPreferredWidth(110);
        userTable.getColumnModel().getColumn(2).setPreferredWidth(70);
        userTable.getColumnModel().getColumn(2).setCellRenderer(cellRenderer1);
        userTable.getColumnModel().getColumn(3).setPreferredWidth(70);
        userTable.getColumnModel().getColumn(3).setCellRenderer(cellRenderer2);
        userTable.getColumnModel().getColumn(4).setPreferredWidth(150);
        userTable.getColumnModel().getColumn(5).setPreferredWidth(220);

//        departmentComboBoxModel = new ViewDepartmentComboBoxModel(mainFrame.getGenericDriver().showDepartment());
//        Department viewAll = new Department(0, "All", null, null, null, null);
//        departmentComboBoxModel.addElement(viewAll);
//        departmentComboBoxModel.setSelectedItem(viewAll);
//        cbDep.setModel(departmentComboBoxModel);
//
//        DefaultComboBoxModel<String> roleModel = new DefaultComboBoxModel<String>(roles);
//        roleModel.setSelectedItem(roles[0]);
//        cbRole.setModel(roleModel);
//        cbLevel.setModel(new DefaultComboBoxModel<String>(levels[0]));
//        cbLevel.setSelectedIndex(0);

        setLoginPanel = new SetUserLoginPanel(mainFrame, this);
    }

    private void swithAction() {
        int choise = cbActions.getSelectedIndex();
        switchTarget(choise);
        cbActions.setSelectedIndex(0);
    }

    private void switchTarget(int choise) {
        if (target != choise || target == 4) {
            switch (choise) {
                case 0:
                    break;
                case 1:
                    this.remove(viewControl);
                    viewControl = new JScrollPane(viewUserPanel);
                    target = 1;
                    this.add(viewControl, BorderLayout.CENTER);
                    this.revalidate();
                    break;
                case 2:
                    new Thread() {

                        @Override
                        public void run() {
                            showSetLogin();
                        }
                    }.start();
                    break;
            }
        }
    }

    public void showView() {
        switchTarget(1);
    }

    private void showSetLogin() {
        if (selectedUser != null) {
            this.remove(viewControl);
            setLoginPanel.setSelectedUser(selectedUser);
            viewControl = new JScrollPane(setLoginPanel);
            target = 2;
            this.add(viewControl, BorderLayout.CENTER);
            this.revalidate();
        } else {
            JOptionPane.showMessageDialog(mainFrame, "You migh select one user !");
        }
    }

//    private void switchLevels() {
//        int index = cbRole.getSelectedIndex();
//        cbLevel.setModel(new DefaultComboBoxModel<String>(levels[index]));
//        cbLevel.setSelectedIndex(0);
//    }
    private void filter() {
//        Department dep = (Department) cbDep.getSelectedItem();
//        userTableModel.fillter(txtLoginID.getText(), txtName.getText(), dep.getDepartmentID(), cbRole.getSelectedIndex(), cbLevel.getSelectedIndex());
        userTableModel.fillter(txtLoginID.getText(), null, 0, 0, 0);
    }

    public boolean setUserLogin(int uid, String pass, int status) {
        boolean returnVal = driver.setLogin(uid, pass, status);
        if (returnVal) {
            selectedUser.setUserID(uid);
            selectedUser.setLoginPass(pass);
            selectedUser.setStatus(status);
            userTableModel.update(selectedUser);
        }
        return returnVal;
    }

    private void setSelectedUser(java.awt.event.MouseEvent evt) {
        int index = userTable.getSelectedRow();
        if (index != -1) {
            selectedUser = userTableModel.getAt(index);
            if (evt.getClickCount() == 2) {
                ViewUserDialog dialog = new ViewUserDialog(mainFrame);
                dialog.setSelectedUser(selectedUser);
                dialog.setLocationRelativeTo(null);
                dialog.setVisible(true);
            }
        }
    }

    public void closeTarget() {
        switchTarget(1);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        cbActions = new javax.swing.JComboBox();
        viewControl = new javax.swing.JScrollPane();
        viewUserPanel = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        userTable = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        txtLoginID = new javax.swing.JTextField();

        setBackground(new java.awt.Color(255, 255, 255));
        setLayout(new java.awt.BorderLayout());

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        cbActions.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Action", "View", "Set Login" }));
        cbActions.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbActionsActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(435, Short.MAX_VALUE)
                .addComponent(cbActions, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cbActions, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        add(jPanel1, java.awt.BorderLayout.PAGE_START);

        viewControl.setBorder(null);

        viewUserPanel.setBackground(new java.awt.Color(255, 255, 255));

        userTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        userTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                userTableMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(userTable);

        jLabel1.setText("LoginID:");

        txtLoginID.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtLoginIDCaretUpdate(evt);
            }
        });

        javax.swing.GroupLayout viewUserPanelLayout = new javax.swing.GroupLayout(viewUserPanel);
        viewUserPanel.setLayout(viewUserPanelLayout);
        viewUserPanelLayout.setHorizontalGroup(
            viewUserPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(viewUserPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(viewUserPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 600, Short.MAX_VALUE)
                    .addGroup(viewUserPanelLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtLoginID, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        viewUserPanelLayout.setVerticalGroup(
            viewUserPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(viewUserPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(viewUserPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtLoginID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 291, Short.MAX_VALUE)
                .addContainerGap())
        );

        viewControl.setViewportView(viewUserPanel);

        add(viewControl, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

private void txtLoginIDCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtLoginIDCaretUpdate
    filter();
}//GEN-LAST:event_txtLoginIDCaretUpdate

private void cbActionsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbActionsActionPerformed
    swithAction();
}//GEN-LAST:event_cbActionsActionPerformed

private void userTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_userTableMouseClicked
    setSelectedUser(evt);
}//GEN-LAST:event_userTableMouseClicked
    private RemedySchedulerFrame mainFrame;
    private AdminSysDriver driver;
    private EditUserLoginTableModel userTableModel;
    private ViewDepartmentComboBoxModel departmentComboBoxModel;
    private SetUserLoginPanel setLoginPanel;
    private static String[] roles = {"All", "Manager", "Administractor", "Department Head", "Employee"};
    private static String[][] levels = {{""}, {""}, {"All", "Head", "Employee"},
        {"All", "Head", "Immediate Head", "Next Immediate Head", "Junior Head "},
        {"All", "Doctor", "Staff"}};
    private User selectedUser;
    private int target;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbActions;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField txtLoginID;
    private javax.swing.JTable userTable;
    private javax.swing.JScrollPane viewControl;
    private javax.swing.JPanel viewUserPanel;
    // End of variables declaration//GEN-END:variables
}
